using System;   
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// A template for GridView columns which contain a label and a textbox
/// </summary>
public class GridViewTemplate : System.Web.UI.Page, ITemplate {
    private ListItemType templateType;
    private string columnName,
                   friendlyName;
    private bool defaultLanguage;

    /// <summary>
    /// Initializes a new GridViewTemplate for the specified column name
    /// </summary>
    /// <param name="type">The field type.</param>
    /// <param name="colname">The name of the column.</param>
    public GridViewTemplate(ListItemType type, string colname, bool defaultLanguage) {
        templateType = type;
        columnName = colname;
        this.defaultLanguage = defaultLanguage;
        try {
            friendlyName = System.Globalization.CultureInfo.GetCultureInfo(columnName).EnglishName;
        } catch {
            friendlyName = columnName;
        }
    }

    /// <summary>
    /// Adds the appropriate controls to a container
    /// </summary>
    /// <param name="container">The <see cref="T:System.Web.UI.Control"/> object to contain the instances of controls from the inline template.</param>
    public void InstantiateIn(System.Web.UI.Control container) {
        Label lbl;
        TextBox txt;
        switch(templateType) {
            case ListItemType.Header:
                lbl = new Label();
                lbl.Text = friendlyName + " (<small>" + columnName + "</small>)";
                lbl.EnableViewState = false;
                container.Controls.Add(lbl);
                break;
            case ListItemType.Item:
                MultiView viw = new MultiView();
                    View viwShow = new View();
                    viw.Views.Add(viwShow);
                    lbl = new Label();
                        lbl.EnableViewState = false;
                        lbl.DataBinding += new EventHandler(lbl_DataBinding);
                    viwShow.Controls.Add(lbl);
                    View viwEdit = new View();
                    txt = new TextBox();
                        txt.ID = columnName;
                        txt.TextMode = TextBoxMode.MultiLine;
                        txt.Rows = 4;
                        txt.EnableViewState = false;
                        txt.ValidationGroup = "editelement";
                        viwEdit.Controls.Add(txt);
                        txt.DataBinding += new EventHandler(txt_DataBinding);
                    Literal br = new Literal();
                        br.Text = "<br />";
                        viwEdit.Controls.Add(br);
                    HyperLink btnFindTranslation = new HyperLink();
                        btnFindTranslation.Text = "Find Similar Translations";
                        btnFindTranslation.NavigateUrl = "#"+columnName; // pass the column name here for JQuery hookup in grd_preRender
                        btnFindTranslation.CssClass = "findTranslation";
                        btnFindTranslation.ToolTip = "Lookup translations for other languages using " + friendlyName;
                        btnFindTranslation.DataBinding += new EventHandler(btnFindTranslation_DataBinding);
                    viwEdit.Controls.Add(btnFindTranslation);
                    if(defaultLanguage) {
                        RequiredFieldValidator val = new RequiredFieldValidator();
                        val.ControlToValidate = txt.ID;
                        val.ErrorMessage = "The default language must be entered";
                        val.SetFocusOnError = true;
                        val.Display = ValidatorDisplay.Dynamic;
                        val.ValidationGroup = "editelement";
                        viwEdit.Controls.Add(val);
                    }
                    viw.Views.Add(viwEdit);
                viw.ActiveViewIndex = 0;
                container.Controls.Add(viw);
                break;
        }
    }

    /// <summary>
    /// Databinds the textbox and assigns it an ID based on the column name.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void txt_DataBinding(object sender, EventArgs e) {
        TextBox txt = sender as TextBox;
        GridViewRow row = txt.NamingContainer as GridViewRow;
        object dataValue = DataBinder.Eval(row.DataItem, columnName);
        if(dataValue != DBNull.Value) {
            txt.Text = dataValue.ToString();
        }
    }

    /// <summary>
    /// Databinds the label.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void lbl_DataBinding(object sender, EventArgs e) {
        Label lbl = sender as Label;
        GridViewRow row = lbl.NamingContainer as GridViewRow;
        object dataValue = DataBinder.Eval(row.DataItem, columnName);
        if(dataValue != DBNull.Value) {
            lbl.Text = dataValue.ToString();
        }
    }

    /// <summary>
    /// Sets the tooltip text and command argument for the 'find translation' button.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void btnFindTranslation_DataBinding(object sender, EventArgs e) {
        HyperLink btn = sender as HyperLink;
        GridViewRow row = btn.NamingContainer as GridViewRow;
        btn.ID = "dynfind-"+ row.DataItemIndex+'-'+ columnName;
    }
}